package top.ztf.erp;

import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Set;



public class ValidateTestClass {

    /**
     * 验证参数：就是验证上述注解的完整方法
     * @return
     */
    @Test
    public void validateParams() {
        //调用JSR303验证工具，校验参数
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Stu stu = new Stu("null","");

        Set<ConstraintViolation<Stu>> violations = validator.validate(stu);
        for (ConstraintViolation<Stu> violation : violations) {
            System.out.println(violation.getPropertyPath());
            System.out.println(violation.getMessage());
        }
    }
}

class Stu{

    @NotNull
    private String password;
    @NotNull
    @Size(min = 1,max = 6,message = "至少三位,最多6位")
    private String name;


    public Stu(String password, String name) {
        this.password = password;
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}




